package com.xrui.hbaseview.filters;

import java.util.regex.Pattern;

/**
 * This class represents an filter based on a regular expression
 */
public class PatternFilter implements Filter {

    private Pattern regex;
    private boolean isNegative;

    public PatternFilter(String regex) {
        this.isNegative = regex.startsWith("~");

        if (this.isNegative) {
            this.regex = Pattern.compile(regex.substring(2, regex.length() - 1));
        } else {
            this.regex = Pattern.compile(regex);
        }
    }

    @Override
    public boolean match(String value) {
        boolean result = this.regex.matcher(value).find();
        if (isNegative) {
            return !result;
        }
        return result;
    }
}
